//
//  AnalystHomeViewController.m
//  RedManStock
//
//  Created by lanou3g on 15/7/22.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "AnalystHomeViewController.h"
#import "AnalystHistoryCell.h"
#import "AnalystHomeHeadCell.h"
#import "AnalystScoreCell.h"
#import "AnalystViewPoints.h"
#import "AnalystArticleViewController.h"
#import "MBProgressHUD.h"
@interface AnalystHomeViewController ()

@property (nonatomic,retain) NSMutableArray *analystViewPointArray;
@property (nonatomic,retain) MBProgressHUD *hud;
@end

@implementation AnalystHomeViewController

//http://api.gongniucaijing.com/analyst/info?id=1069&idfa=7B7C9B42-5C18-4EEB-A0F8-6708DE081AAE&machine=iPhone%205&number=10&offset=0&os=iPhone%20OS%208.2&platform=1&protocolVersion=1.0&qudao=0000001&resolution=640%2A1136&token=d58f02522dac7e698a4600c8b18ff02c0b9fa6df&uuid=BULLSTOCK-79460017-C3E5-45EC-B98C-1DD541A61127&version=1.9.0&versionCode=190

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.update=YES;
    self.analystViewPointArray=[NSMutableArray array];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AnalystHistoryCell" bundle:nil] forCellReuseIdentifier:@"AnalystHistoryCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"AnalystHomeHeadCell" bundle:nil] forCellReuseIdentifier:@"AnalystHomeHeadCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"AnalystScoreCell" bundle:nil] forCellReuseIdentifier:@"AnalystScoreCell"];
    
    [self loadNewData];
    
}

- (void)loadNewData{
    self.hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.yOffset=-60;
    NSString *urlStr=@"http://api.gongniucaijing.com/analyst/info";
    [[MyHandle new] requestDataWithURLStr:urlStr parameterBlock:^NSDictionary *{
        NSDictionary *dic=@{@"id":self.analyst.analyst_id,
                            @"number=10":@"10",
                            @"offset":@"0"};
        return dic;
    } successBlock:^(NSDictionary *responseDic) {
        
        NSArray *array1=[[responseDic objectForKey:@"data"] objectForKey:@"lists"];
        
        NSMutableArray *tempArray=[NSMutableArray array];
        for (NSDictionary *dic in array1) {
            
            AnalystViewPoints *viewPoint=[[AnalystViewPoints alloc]init];
            [viewPoint setValuesForKeysWithDictionary:dic];

            [tempArray addObject:viewPoint];
            
        }
        self.analystViewPointArray=tempArray;
        
#warning 要把 这个viewpointsArray赋值给analyst中的viewPoints属性
        self.analyst.viewPointsArray=self.analystViewPointArray;
        
        [self.tableView reloadData];
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [self.tableView.header endRefreshing];
        
    } errorBlock:^(NSError *error) {
        NSLog(@"error=%@",error);
    }];
    
}

-(void)loadMoreData{
    
    NSString *urlStr=@"http://api.gongniucaijing.com/analyst/info";
    [[MyHandle new] requestDataWithURLStr:urlStr parameterBlock:^NSDictionary *{
        NSDictionary *dic=@{@"id":self.analyst.analyst_id,
                            @"number=10":@"10",
                            @"offset":[NSString stringWithFormat:@"%lu",self.analystViewPointArray.count]};
        return dic;
    } successBlock:^(NSDictionary *responseDic) {
        
        NSArray *array1=[[responseDic objectForKey:@"data"] objectForKey:@"lists"];
        
        NSMutableArray *tempArray=[NSMutableArray array];
        for (NSDictionary *dic in array1) {
            
            AnalystViewPoints *viewPoint=[[AnalystViewPoints alloc]init];
            [viewPoint setValuesForKeysWithDictionary:dic];
            
            [tempArray addObject:viewPoint];
            
        }
        [self.analystViewPointArray addObjectsFromArray:tempArray];
#warning 还是要直接赋值
        self.analyst.viewPointsArray=self.analystViewPointArray;
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        
    } errorBlock:^(NSError *error) {
        NSLog(@"error=%@",error);
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    CGRect frame=CGRectMake(0, 0, self.view.bounds.size.width, 20);
    
    if (section==1) {
        UILabel *label1=[UILabel labelWithFrame:frame fontSize:15 lineBreak:YES];
        label1.text=@"荐股道行";
        label1.backgroundColor=[UIColor grayColor];
        label1.textAlignment=NSTextAlignmentCenter;
        return label1;
    }
    else if(section==2){
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
        
        UILabel *label1=[UILabel labelWithFrame:frame fontSize:15 lineBreak:YES];
        label1.text=@"历次荐股";
        label1.backgroundColor=[UIColor grayColor];
        label1.textAlignment=NSTextAlignmentCenter;
        UILabel *label2=[UILabel labelWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 15) fontSize:14 lineBreak:YES];
//        label2.layer.borderWidth=1;
//        label2.layer.borderColor=[UIColor grayColor].CGColor;
        label2.backgroundColor=[UIColor whiteColor];
        label2.textAlignment=NSTextAlignmentCenter;
        label2.text=@"荐股日期        股票名称       荐股周均涨幅";
        [view addSubview:label1];
        [view addSubview:label2];
        return view;
    }
    else{
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        return 100;
    }
    else if(indexPath.section==1){
        return 80;
    }
    else{
        return 40;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0;
    }
    else if(section==1){
        return 20;
    }
    else{
        return 35;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    else if(section==1){
        return 1;
    }
    else{
        return self.analystViewPointArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        AnalystHomeHeadCell *cell=[tableView dequeueReusableCellWithIdentifier:@"AnalystHomeHeadCell" forIndexPath:indexPath];
        
        cell.analyst=self.analyst;
        return cell;
    }
    else if (indexPath.section==1){
        
        AnalystScoreCell *cell=[tableView dequeueReusableCellWithIdentifier:@"AnalystScoreCell" forIndexPath:indexPath];
        cell.analyst=self.analyst;
        return cell;
    }
    else{
        
        AnalystHistoryCell *cell=[tableView dequeueReusableCellWithIdentifier:@"AnalystHistoryCell" forIndexPath:indexPath];
        
#pragma mark  这里要留意，viewPointArray的数据源已经赋给了analyst的array属性，但是!!!!!!! 千万不能修改，analyst的analystViewpoints属性，不然会改变首页的数据! 因为指向的是同一块内存
        AnalystViewPoints *viewPoint=self.analystViewPointArray[indexPath.row];
#pragma mark 这里创建临时的tempAnalyst（其他属性都没有值），把数据源中取出的viewpoint赋给tempAnalyst的属性，因为这个Cell，只需要viewPoint属性
        Analyst *tempAnalyst=[[Analyst alloc]init];
        tempAnalyst.viewPointSB=viewPoint;
        cell.analyst=tempAnalyst;
        return cell;
    }
    
}



//这里只需要跳转section=2的页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==2) {
        
        AnalystArticleViewController *analystArticleVC=[[AnalystArticleViewController alloc]init];
        
        AnalystViewPoints *viewPoint=self.analystViewPointArray[indexPath.row];
        
        analystArticleVC.webUrl=viewPoint.url;
        
        [self.navigationController pushViewController:analystArticleVC animated:YES];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
